﻿using DevExpress.XtraCharts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using T4Airline.Entities;

namespace T4Airline.Website
{
	public partial class _Default : Page
	{

		private int departureWindSpeed=0;
		private int DepartureWindSpeed
		{
			get 
			{
				HttpCookie myCookie = new HttpCookie("windSpeedDeparture");
				myCookie = Request.Cookies["windSpeedDeparture"];
			
				if (myCookie != null)
				{

					Int32.TryParse(myCookie.Value, out departureWindSpeed);
				}
				return departureWindSpeed;
			}
		}


		private int arrivalWindSpeed = 0;
		private int ArrivalWindSpeed
		{
			get
			{
				HttpCookie myCookie = new HttpCookie("windSpeedArrival");
				myCookie = Request.Cookies["windSpeedArrival"];

				if (myCookie != null)
				{

					Int32.TryParse(myCookie.Value, out arrivalWindSpeed);
				}
				return arrivalWindSpeed;
			}
		}
		protected void Page_Load(object sender, EventArgs e)
		{

			// Removes past dates from calendar and limits future dates to Now + 5 days
			SetCalendarControl();
			BindRouteGridData();
	
		}


		/// <summary>
		/// Sets the minumum and maximum dates of the calendar control.
		/// Only allows 5 days out due to limits on weather data
		/// Disables all past dates
		/// </summary>
		private void SetCalendarControl()
		{
			if (deDate.Date < DateTime.Now)
				deDate.Date = DateTime.Now;

			deDate.MaxDate = DateTime.Today.AddDays(4);
			deDate.MinDate = DateTime.Today;
		}

		private void BindRouteGridData()
		{
			if (ASPxPageControl1.ActiveTabIndex != 0)
			{

				var routeList = new List<RouteData>();
				if (grdFrom.Value != null && grdTo.Value != null)
				{
					int originAirportID = (int)grdFrom.Value;
					int destinationAirportID = (int)grdTo.Value;

					using (var db = new team4AirlineEntities())
					{
						var matchedRoutes = (from x in db.Routes where x.OriginAirportID == originAirportID && x.DestinationAirportID == destinationAirportID select x).ToList();

						foreach (Route rt in matchedRoutes)
						{
							var rd = new RouteData();
							rd.ArrivalAirport = rt.ArrivalAirport;
							rd.DepartureAirport = rt.DepartureAirport;
							rd.AirlineSelected = rt.Airline;
							rd.EstimatedMinutesDepartureDelayed = FAAFlightData.FlightDepartureDelayPercentChance(rd.DepartureAirport.ID, rd.ArrivalAirport.ID,rd.AirlineSelected.ID,DepartureWindSpeed);
							rd.EstimatedMinutesArrivalDelayed = FAAFlightData.FlightArrivalDelayPercentChance(rd.DepartureAirport.ID, rd.ArrivalAirport.ID, rd.AirlineSelected.ID, ArrivalWindSpeed);
							routeList.Add(rd);
						}

						if (routeList.Count == 0 )
						{
							var connections = FAAFlightData.GetConnectingAirports(originAirportID, destinationAirportID);

							foreach(Airport connectingAirport in connections)
							{
								var matchedRoutes1 = (from x in db.Routes where x.OriginAirportID == originAirportID && x.DestinationAirportID == connectingAirport.ID select x).FirstOrDefault();
								var matchedRoutes2 = (from x in db.Routes where x.OriginAirportID == connectingAirport.ID && x.DestinationAirportID == destinationAirportID && x.AirlineID == matchedRoutes1.AirlineID   select x).FirstOrDefault();


								if (matchedRoutes1 != null && matchedRoutes2 != null)
								{
									var rd = new RouteData();
									rd.ArrivalAirport = matchedRoutes2.ArrivalAirport;
									rd.DepartureAirport = matchedRoutes1.DepartureAirport;
									rd.AirlineSelected = matchedRoutes2.Airline;
									rd.ConnectingAirport = connectingAirport;
									rd.EstimatedMinutesDepartureDelayed = FAAFlightData.FlightDepartureDelayPercentChance(rd.DepartureAirport.ID, rd.ArrivalAirport.ID, rd.AirlineSelected.ID, rd.ConnectingAirport.ID, DepartureWindSpeed);
									rd.EstimatedMinutesArrivalDelayed = FAAFlightData.FlightArrivalDelayPercentChance(rd.ConnectingAirport.ID, rd.ArrivalAirport.ID, rd.AirlineSelected.ID, ArrivalWindSpeed);
									routeList.Add(rd);
								}
							}


						}
					}
				}
				routeList = (from x in routeList orderby x.EstimatedMinutesDepartureDelayed select x).ToList();
				WebChartControl1.DataSource = routeList;

				WebChartControl1.Series[0].ArgumentDataMember = "ChartCaption";
				WebChartControl1.Series[0].ValueDataMembers[0] = "EstimatedMinutesDepartureDelayed";
				WebChartControl1.Series[1].ArgumentDataMember = "ChartCaption";
				WebChartControl1.Series[1].ValueDataMembers[0] = "EstimatedMinutesArrivalDelayed";
				WebChartControl1.DataBind();

				grdRoutes.DataSource = routeList;
				grdRoutes.DataBind();
				if (grdRoutes.VisibleRowCount == 0) grdRoutes.Width = 600;
			}

		}



	}
}